/*
 * @Description: 路由参数练习
 * @Author: wangqi
 * @Date: 2024-07-20 19:10:58
 * @LastEditTime: 2024-07-23 16:08:44
 * @LastEditAuthor: wangqi
 */

/**
 * 
 *  参数概念总览：
 *   req：
 * - method   
 * - url
 * - params
 * - query
 */
const fs = require('fs');
const path = require('path');
const express = require('express')
const app = express()
const logger = require("./_unit/logs");
const singersData = require("./data/singers.json")

app.get("/", (req, res) => {
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
    res.end("歌曲信息")
})

app.get("/:id.html", (req, res) => {
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
    const { id } = req.params
    let result = singersData.singers.find((item) => id == item.id)
    let strHtml;

    if (!result) {
        strHtml = `<h2>404 未找到!</h2>`
        res.end(strHtml)
    }

    /* 这种方式怎么将参数传递给模板文件 */
    // let fileBuff = fs.readFileSync(path.join(__dirname, 'test/02_路由参数.html'))
    // let strHtml = fileBuff.toString()

    strHtml = `<h2>歌手名称：${result.singer_name}</h2><img src="${result.singer_pic}">`

    res.end(strHtml)
})


app.listen(3000, () => {
    console.log("启动3000端口!")
})


